package com.siemens.industrial.web.mvc.controller.admin;

import com.siemens.industrial.model.admin.*;
import com.siemens.industrial.utils.Definition;
import com.siemens.industrial.utils.GlobalResponseCode;
import com.siemens.industrial.utils.RestResponse;
import com.siemens.industrial.web.mvc.pojo.DataTableQuery;
import com.siemens.industrial.web.mvc.pojo.TenantServiceItem;
import com.siemens.industrial.web.mvc.service.AuthService;
import com.siemens.industrial.web.mvc.service.admin.DeviceService;
import com.siemens.industrial.web.mvc.service.admin.ServeService;
import com.siemens.industrial.web.mvc.service.admin.TenantService;
import com.siemens.industrial.web.mvc.service.admin.TenantServiceRelationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qupen on 2016/9/21.
 */
@Controller
public class ServiceController {
    @Resource
    ServeService serveService;

    @Resource
    TenantService tenantService;

    @Resource
    DeviceService deviceService;

    @Resource
    TenantServiceRelationService tenantServiceRelationService;

    @Resource
    AuthService authService;

    @RequestMapping(value = "/admin/service/list", method = RequestMethod.GET)
    public String getUserListPage() {
        return "admin/service_list";
    }

    @RequestMapping(value = "/admin/api/v1/service/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getUserList() {
        List<Service> services = serveService.getServiceList();

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(services).build();
    }

    @RequestMapping(value = "/admin/service/manage", method = RequestMethod.GET)
    public String getServerManageListPage() {
        return "admin/server_manage";
    }

    @RequestMapping(value = "/admin/api/v1/service/manages", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getServerManageList() {
        List<TenantServiceItem> tenantServiceItems = serveService.getServiceManageList();

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(tenantServiceItems).build();
    }

    @RequestMapping(value = "/admin/service/manageEdit", method = RequestMethod.GET)
    public String getServerProjectEditPage(String tenantId,String serviceId, ModelMap modelMap) {
        List<Service> serviceList=serveService.getServiceList();
        Tenant tenant=tenantService.getTenant(tenantId);
        int count=deviceService.getDeviceConut(tenantId);
        modelMap.put("services", serviceList);
        modelMap.put("tenant", tenant);
        modelMap.put("deviceCount",count);
        modelMap.put("serviceId",serviceId);
        return "admin/service_project_edit";
    }

    /**
     * 服务订阅统计
     *
     * @return
     */
    @RequestMapping(value = "/admin/api/v1/service/statistics", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getServerManageCount() {
        Map<String,Object> serverStatistics = serveService.getManageCount();

        return RestResponse.create(GlobalResponseCode.SUCCESS).putData(serverStatistics).build();
    }

    @RequestMapping(value = "/admin/service/statistics", method = RequestMethod.GET)
    public String getServerManagePage() {
        return "/admin/serverSubscriptionStatistics";
    }

    @RequestMapping(value = "/admin/api/v1/service/setEnabled", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setServiceEnabled(@RequestParam(name = "serviceId") String serviceId, @RequestParam(name = "enabled") int enabled) {
        boolean isSuccess = serveService.setServiceEnabled(serviceId, enabled);
        if (isSuccess) {
            return RestResponse.create(GlobalResponseCode.SUCCESS).build();
        }
        return new HashMap<>();
    }

    @RequestMapping(value = "/admin/api/v1/service/extend",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> extendServiceDate(@RequestParam(name = "tenantId") String tenantId,
                                                @RequestParam(name = "services[]",required = false) String [] services,
                                                @RequestParam(name = "date") int mouth){

        tenantServiceRelationService.setTenantService(tenantId,services,mouth);

        return RestResponse.create(GlobalResponseCode.SUCCESS).build();
    }

    @RequestMapping(value = "/admin/api/v1/service/managesByDataTable", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getServerManageListByDataTable(HttpServletRequest request) {
        DataTableQuery dt = new DataTableQuery(request);
        if (dt.isDataTableRequest()) {
            Map<String, Object> tenantServiceItems = serveService.getServiceManageListByDataTable(request);
            return tenantServiceItems;
        }else {
            return dt.buildResponse(0, 0, new ArrayList<>());
        }
    }

    //dataTable search service list
    @RequestMapping(value = "/admin/api/v1/service/listByDataTable", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getUserListByDataTable(HttpServletRequest request) {
        DataTableQuery dt = new DataTableQuery(request);
        if (dt.isDataTableRequest()) {
            Map<String,Object> services = serveService.getServiceListByDataTable(request);
            return services;
        }else {
            return dt.buildResponse(0, 0, new ArrayList<>());
        }
    }
}
